`ifndef __SCRAMBLER__
`define __SCRAMBLER__

module scrambler (
           input clk,
           input rst_n,
           input [6: 0] seed,
           input load,
           input din,
           input din_valid,
           output dout,
           output dout_valid);

`define LFSR_init 7'b1011101

reg dout_reg, dout_valid_reg;
reg [6: 0] LFSR;

assign dout = dout_reg;
assign dout_valid = dout_valid_reg;

always @(posedge clk or negedge rst_n) begin
    if (rst_n == 0) begin // 异步执行程序，因此单独判定
        dout_valid_reg <= 0;
        dout_reg <= 0;
        LFSR <= `LFSR_init;
    end
    else begin // 剩下为时钟触发同步程序
        if (din_valid == 1) begin
            dout_valid_reg <= 1;
            dout_reg <= ((LFSR[6] ^ LFSR[3]) ^ din);
            LFSR <= LFSR << 1;
            LFSR[0] <= (LFSR[6] ^ LFSR[3]);
        end else begin // 解除空判定
            dout_valid_reg <= 0;
            dout_reg <= 0;
        end

        if (load == 1) begin
            LFSR <= seed;
            dout_valid_reg <= 0;
            dout_reg <= 0;
        end else begin // 解除空判定
            
        end
    end

end

endmodule

`endif